package com.dp.iot.admin.api.iotequipmentcollection;

import com.dp.iot.core.service.CommonRestController;
import org.springframework.beans.factory.InitializingBean;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.Date;
import java.sql.Time;
import java.sql.Timestamp;
import java.math.*;
import java.util.List;
import java.util.Map;
import com.dp.iot.core.common.constant.PageConstant;
import com.dp.iot.core.common.exception.BusinessException;
import com.dp.iot.core.serialize.ResponseMsg;
import com.dp.iot.model.IotEquipmentCollection;
import com.dp.iot.service.iotequipmentcollection.service.IotEquipmentCollectionService;

import com.dp.iot.core.utils.ListUtil;
import com.dp.iot.core.utils.MapUtil;
import com.dp.iot.core.utils.StringUtil;


/**
* 代码为自动生成 Created by www.magicalcoder.com
* 如果你改变了此类 read 请将此行删除
* 欢迎加入官方QQ群:323237052
*/

@RequestMapping("/admin/iot_equipment_collection_rest/")
@RestController
public class AdminIotEquipmentCollectionRestController extends CommonRestController<IotEquipmentCollection,String> implements InitializingBean
{

    @Resource
    private IotEquipmentCollectionService iotEquipmentCollectionService;

    @RequestMapping(value = "search")
    public ResponseMsg search(
        @RequestParam(required = false) String uniqueField,
        @RequestParam(required = false) String uniqueValue,
        @RequestParam(required = false,defaultValue = "20") Integer limit,
        @RequestParam(required = false) String keyword
    ){
        limit = Math.min(PageConstant.MAX_LIMIT,limit);
        List<IotEquipmentCollection> list = null;
        Map<String,Object> query = new HashedMap();
        query.put("limit",limit);
        query.put("notSafeOrderBy","equipment_collection_id desc");
        if(uniqueValue!=null){//说明是来初始化的
            query.put(uniqueField,uniqueValue);
            list = iotEquipmentCollectionService.getModelList(query);
        }else {//正常搜索
            if(ListUtil.isBlank(list)){
                query.put("equipmentIdFirst",keyword);
                list = iotEquipmentCollectionService.getModelList(query);
                query.remove("equipmentIdFirst");
            }
            if(ListUtil.isBlank(list)){
                query.put("collectionModeIdFirst",keyword);
                list = iotEquipmentCollectionService.getModelList(query);
                query.remove("collectionModeIdFirst");
            }
            if(ListUtil.isBlank(list)){
                query.put("collectionIpFirst",keyword);
                list = iotEquipmentCollectionService.getModelList(query);
                query.remove("collectionIpFirst");
            }
            if(ListUtil.isBlank(list)){
                query.put("equipmentCollectionStatusFirst",keyword);
                list = iotEquipmentCollectionService.getModelList(query);
                query.remove("equipmentCollectionStatusFirst");
            }
            if(ListUtil.isBlank(list)){
                query.put("equipmentCollectionTypeFirst",keyword);
                list = iotEquipmentCollectionService.getModelList(query);
                query.remove("equipmentCollectionTypeFirst");
            }
            if(ListUtil.isBlank(list)){
                query.put("remarksFirst",keyword);
                list = iotEquipmentCollectionService.getModelList(query);
                query.remove("remarksFirst");
            }
            if(ListUtil.isBlank(list)){
                query.put("creationUserFirst",keyword);
                list = iotEquipmentCollectionService.getModelList(query);
                query.remove("creationUserFirst");
            }
        }
        return new ResponseMsg(list);
    }
    //分页查询
    @RequestMapping(value={"page"}, method={RequestMethod.GET})
    public ResponseMsg page(
        @RequestParam(required = false,value ="equipmentCollectionIdFirst")                            String equipmentCollectionIdFirst ,
        @RequestParam(required = false,value ="equipmentIdFirst")                            String equipmentIdFirst ,
        @RequestParam(required = false,value ="collectionModeIdFirst")                            String collectionModeIdFirst ,
        @RequestParam(required = false,value ="collectionIpFirst")                            String collectionIpFirst ,
        @RequestParam(required = false,value ="equipmentCollectionStatusFirst")                            String equipmentCollectionStatusFirst ,
        @RequestParam(required = false,value ="equipmentCollectionTypeFirst")                            String equipmentCollectionTypeFirst ,
        @RequestParam(required = false,value ="remarksFirst")                            String remarksFirst ,
        @RequestParam(required = false,value ="creationTimeFirst")                    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")Date creationTimeFirst ,
        @RequestParam(required = false,value ="creationTimeSecond")                    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")Date creationTimeSecond ,
        @RequestParam(required = false,value ="creationUserFirst")                            String creationUserFirst ,
        @RequestParam int page,@RequestParam int limit,@RequestParam(required = false) String safeOrderBy)
    {
        limit = Math.min(limit, PageConstant.MAX_LIMIT);
        int start = (page - 1) * limit;
        Map<String,Object> query = new HashedMap();
        query.put("equipmentCollectionIdFirst",coverBlankToNull(equipmentCollectionIdFirst));
        query.put("equipmentIdFirst",coverBlankToNull(equipmentIdFirst));
        query.put("collectionModeIdFirst",coverBlankToNull(collectionModeIdFirst));
        query.put("collectionIpFirst",coverBlankToNull(collectionIpFirst));
        query.put("equipmentCollectionStatusFirst",coverBlankToNull(equipmentCollectionStatusFirst));
        query.put("equipmentCollectionTypeFirst",coverBlankToNull(equipmentCollectionTypeFirst));
        query.put("remarksFirst",coverBlankToNull(remarksFirst));
        query.put("creationTimeFirst",creationTimeFirst);
        query.put("creationTimeSecond",creationTimeSecond);
        query.put("creationUserFirst",coverBlankToNull(creationUserFirst));
        Integer count = iotEquipmentCollectionService.getModelListCount(query);
        query.put("start",start);
        query.put("limit",limit);
        if(StringUtil.isBlank(safeOrderBy)){
            query.put("notSafeOrderBy","equipment_collection_id desc");
        }else{
            query.put("safeOrderBy",safeOrderBy);
        }
        return new ResponseMsg(count,iotEquipmentCollectionService.getModelList(query));
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        super.commonService = iotEquipmentCollectionService;
        super.primaryKey = "equipmentCollectionId";//硬编码此实体的主键名称
    }
}
